#include "stdafx.h"
#include "DoOnSizeBase.h"

DoOnSizeBase::DoOnSizeBase()
{
}

DoOnSizeBase::~DoOnSizeBase()
{
}

void DoOnSizeBase::GetCtrlDefaultMoveType(CtrlInitPos pos, CtrlMoveType& way)
{
	if (pos.isChgCtrl == 1)
	{
		way = CtrlMoveType::CHG_RIGHTDOWN;
	}
	else if (pos.isRightHalf == 1 && pos.isDownHalf == 1)
	{
		way = CtrlMoveType::MV_RIGHTDOWN;
	}
	else if (pos.isRightHalf == 1)
	{
		way = CtrlMoveType::MV_RIGHT;
	}
	else if (pos.isDownHalf == 1)
	{
		way = CtrlMoveType::MV_DOWN;
	}
	else
	{
		way = CtrlMoveType::STOP;
	}
}

void DoOnSizeBase::RegisterInitCtrlPos(int idc, CtrlInitPos pos)
{
	m_mapCtrls[idc] = pos;
}

BOOL DoOnSizeBase::GetInitPosByIDC(int idc, CtrlInitPos &pos)
{
	if (m_mapCtrls.Lookup(idc, pos))
	{
		return TRUE;
	}

	return FALSE;
}

void DoOnSizeBase::GetCtrlPos(bool isChgCtrl, CRect rcChild, RECT rcParent, CtrlInitPos &pos)
{
    double htParent = rcParent.bottom - rcParent.top;
    double wdParent = rcParent.right - rcParent.left;

	pos.x = rcChild.right - rcChild.left;
	pos.y = rcChild.bottom - rcChild.top;
	pos.disBottom = rcParent.bottom - rcChild.bottom;
    pos.ratioBottom = pos.disBottom / htParent;

	pos.disLeft = rcChild.left - rcParent.left;
    pos.ratioLeft = pos.disLeft / wdParent;

	pos.disRight = rcParent.right - rcChild.right;
    pos.ratioRight = pos.disRight / wdParent;

	pos.disTop = rcChild.top - rcParent.top;
    pos.ratioTop = pos.disTop / htParent;

	pos.isChgCtrl = 0;
	pos.isRightHalf = 0;
	pos.isDownHalf = 0;
	if (isChgCtrl)
	{
		pos.isChgCtrl = 1;
	}
	else
	{
		if ((rcChild.left + rcChild.right) / 2 > (rcParent.left + rcParent.right) / 2)
		{
			pos.isRightHalf = 1;
		}

		if ((rcChild.top + rcChild.bottom) / 2 > (rcParent.top + rcParent.bottom) / 2)
		{
			pos.isDownHalf = 1;
		}
	}
}

BOOL CALLBACK DoOnSizeBase::EnumChildProc(HWND hwndChild, LPARAM lParam)
{
	int idChild = ::GetWindowLong(hwndChild, GWL_ID);

	DoOnSizeBase *Dlg = (DoOnSizeBase *)(lParam);
	Dlg->m_idcs.Add(idChild);

	return TRUE;
}
